<?php

namespace app\customer\controller;

use app\BaseController;


class Base extends BaseController
{

    // 初始化方法
    protected function initialize()
    {
        // 调用父类初始化方法
        parent::initialize();

        // 添加其他初始化操作
    }
    /**
     * @describe返回数据助手函数
     * @author: Bobo
     * @date: 2024-06-05 03:38:48 
     * @param [mixed] $data [数据] @required
     * @param [int] $code [状态码] 
     * @param [string] $msg [消息] 
     * @return string 返回的JSON字符串
     */
    protected function returnJsonData($data, $code = 0, $msg = 'success')
    {
        // 判断是否需要自定义状态码和消息
        if (is_numeric($code)) {
            $code = $code;
        }
        if (!empty($msg)) {
            $msg = $msg;
        }

        // 合并数据
        $result = ['code' => $code, 'message' => $msg, 'data' => $data];

        return json($result);
    }

    protected function returnNewJsonData(int $code, array|null $data = [], string $msg = '')
    {
        if (!is_numeric($code)) {
            throw new \think\Exception('The code must be a numeric value.');
        }
        $codeMessage = config()['code'];
        if (!isset($codeMessage[$code])) {
            throw new \think\Exception('The code does not have an associated message.');
        }
        $result = [
            'code' => $code,
            'message' => $codeMessage[$code] . ',' . $msg,
            'data' => $data
        ];
        return json($result);
    }
    /**
     * @describe返回数据助手函数
     * @author: Bobo
     * @date: 2024-06-05 03:38:48 
     * @param [mixed] $data [数据] @required
     * @param [int] $code [状态码] 
     * @param [string] $msg [消息] 
     * @return array 返回的数组
     */
    protected function returnArrayData($data, $code = 0, $msg = 'success'): array
    {
        // 判断是否需要自定义状态码和消息
        if (is_numeric($code)) {
            $code = $code;
        }
        if (!empty($msg)) {
            $msg = $msg;
        }

        // 合并数据
        return ['code' => $code,  'data' => $data, 'message' => $msg];
    }
}
